package com.wyx.config;


import com.bean.BeanFive;
import com.bean.BeanFour;
import com.bean.BeanTree;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class CommonConfig {
    @ConditionalOnProperty(prefix = "bean-tree",name = {"name","icon"}) // 当bean-tree.name和bean-tree.icon存在时注入
    @Bean  // 外部bean注入
    public BeanTree beanTree(@Value("${bean-tree.name}") String name, @Value("${bean-tree.icon}") String icon) { //可通过方法名获取bean
        BeanTree beanTree = new BeanTree();
        beanTree.setName(name);
        beanTree.setIcon(icon);
        return beanTree;
    }
    @Bean
    @ConditionalOnMissingBean(BeanTree.class) // 当没有bean-tree时注入
    public BeanFour beanFour() {
        return new BeanFour();
    }
    @Bean
    @ConditionalOnClass(name = {"com.wyx.bean.BeanTree"}) // 当存在BeanTree时注入
    public BeanFive beanFive() {
        return new BeanFive();
    }
}
